package com.czl.module_storehouse.activity.baseInfo;

import android.os.Bundle;
import android.text.TextUtils;

import com.czl.base.binding.viewadapter.flycoTabLayout.TabEntity;
import com.czl.module_base.activity.BaseTabActivity;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.event.ScanSortEvent;
import com.czl.module_storehouse.fragment.GoodsBaseInfoFragment;
import com.czl.module_storehouse.fragment.StockInfoFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class GoodsBaseInfoActivity extends BaseTabActivity {

    private List<SupportFragment> mFragmentList;
    private String mSortCode;

    @Override
    protected List<SupportFragment> fragments() {
        if (mFragmentList == null) {
            mFragmentList = new ArrayList<>();
            mFragmentList.add(new GoodsBaseInfoFragment());
            mFragmentList.add(new StockInfoFragment());
        }
        return mFragmentList;
    }

    @Override
    protected ArrayList<CustomTabEntity> mTabEntities() {
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        entities.add(new TabEntity("基本信息", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("库存情况", R.mipmap.ic_add, R.mipmap.ic_add));
        return entities;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品详情");

        LiveEventBus.get(ScanSortEvent.class).observe(this, scanSortEvent -> {
            mSortCode = scanSortEvent.getSortCode();
            if (!TextUtils.isEmpty(mSortCode)) {
                for (SupportFragment fragment : fragments()) {
                    if (fragment instanceof GoodsBaseInfoFragment) {
                        ((GoodsBaseInfoFragment) fragment).loadData(null, scanSortEvent.getSortCode());
                    }
                    if (fragment instanceof StockInfoFragment) {
                        ((StockInfoFragment) fragment).setSortCode(mSortCode);
                    }
                }
            } else {
                Integer sortId = scanSortEvent.getSortId();
                for (SupportFragment fragment : fragments()) {
                    if (fragment instanceof GoodsBaseInfoFragment) {
                        ((GoodsBaseInfoFragment) fragment).loadData(sortId, null);
                    }
                    if (fragment instanceof StockInfoFragment) {
                        ((StockInfoFragment) fragment).setSortId(sortId);
                    }
                }
            }

        });
    }
}
